package com.tianyi.controller.wechat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.tianyi.controller.PhotoOutputController;
import com.tianyi.controller.comUser.ComUserController;
import com.tianyi.dao.mapper.OrdersMapper;
import com.tianyi.fileUpload.FileUploadService;
import com.tianyi.lib.bo.wechat.AuthJsToken;
import com.tianyi.lib.model.WebResult;
import com.tianyi.lib.req.BaseReq;
import com.tianyi.lib.req.UserAddrReq;
import com.tianyi.lib.req.UserAuditInfoReq;
import com.tianyi.wechat.WeChatService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.weixin4j.WeixinException;
import org.weixin4j.http.HttpsClient;
import org.weixin4j.http.Response;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.Objects;

/**
 * wechat controller
 *
 * @author cuixuhui
 * @date 2020/05/07
 */
@RestController
@RequestMapping("wechat")
public class WeChatController {

    private static final Logger logger = LoggerFactory.getLogger(WeChatController.class);

    @Autowired
    private WeChatService weChatService;
    @Autowired
    private FileUploadService fileUploadService;
    @Autowired
    private PhotoOutputController photoOutputController;
    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 根据code获取open_id
     * 小程序后台登录，向微信平台发送获取access_token请求，并返回openId
     *
     * @param code
     * @return openid
     * @throws WeixinException
     * @throws IOException
     * @since Weixin4J 1.0.0
     */
    @GetMapping("login")
    public Map<String, Object> login(String code, HttpServletRequest request) throws WeixinException, IOException {
        if (StringUtils.isBlank(code)) {
            throw new WeixinException("invalid null, code is null.");
        }
        Map<String, Object> result = weChatService.login(code);
        return result;
    }

    /**
     * 以流的方式返回给前端
     * @param imgName
     * @param response
     */
    @RequestMapping(value = "/getImg")
    public void getImg(String imgName, HttpServletResponse response) {
        photoOutputController.getSourceImg(imgName, response);
    }

    /**
     * 获取用户信息
     * @param req
     * @return
     */
    @GetMapping("getUserInfo")
    public WebResult<?> getUserInfo(BaseReq req){

        return weChatService.getUserInfo(req);
    }

    /**
     * 用户上传名片
     * @param req
     */
    @RequestMapping(value = "uploadUserBuCardPhoto", method = RequestMethod.POST)
    public WebResult<String> uploadUserBuCardPhoto(UserAuditInfoReq req){

        return weChatService.uploadUserBuCardPhoto(req);
    }

    /**
     * 修改用户信息
     * @param req
     */
    @RequestMapping(value = "updateUserInfo", method = RequestMethod.POST)
    public WebResult<String> updateUserInfo(UserAuditInfoReq req){

        return weChatService.updateUserInfo(req);
    }

    /**
     * 用户新增地址
     * @param req
     * @return
     */
    @RequestMapping(value = "addAddress", method = RequestMethod.POST)
    public WebResult<?> addAddress(UserAddrReq req){

        return weChatService.addAddress(req);
    }

    /**
     * 用户修改地址
     * @param req
     * @return
     */
    @RequestMapping(value = "updateUserAddress", method = RequestMethod.POST)
    public WebResult<?> updateUserAddress(UserAddrReq req){

        return weChatService.updateAddress(req);
    }

    @RequestMapping(value = "queryUserAddressPage", method = RequestMethod.GET)
    public WebResult<?> queryUserAddressPage(UserAddrReq req){

        return weChatService.queryUserAddressPage(req);
    }


}
